local damage = require 'types.damage'
local random = math.pseudo_random

local mt = damage.__index 

mt.damage_type = '自适应'
mt.ignore_defence = false
mt.ignore_magic_resist = true

--function mt:on_init()
--	local damage = self
--	local source = self.source
--	local target = self.target
--end

--护甲因子
mt.def_sub = 0.01
--护甲计算
local dmg_types = {'物理','魔法'}

function mt:get_base_damage()
    local source = self.source
    local target = self.target
    if source:is_ally(target) then
        return 0
    end
    local skill = self.skill
    local rate = 0
    if skill then
        self.damage_type = skill['伤害类型'] or self.damage_type
        if skill['攻击类型'] then
            rate = source:get(skill['攻击类型'] .. '伤害')
        end
    else
        if self:is_attack() then
            self.damage_type = '物理'
            rate = source:get('攻击伤害')
            source:add('生命',source:get('攻击回血'))
        end
    end
    if self.damage_type~='真实' then

        local ignore_def = 0
        local def = 0
        --自适应处理
        if self.hp_remove==nil then
            if self.damage_type=='自适应' then
                local dmg_add = nil
                for _,ty in ipairs(dmg_types) do
                    local new_add = source:get(ty .. '伤害')
                    if dmg_add==nil or new_add>dmg_add then
                        dmg_add = new_add
                        self.damage_type = ty
                    end
                end
                self:mul(dmg_add + rate)
            else
                self:mul(source:get(self.damage_type .. '伤害') + rate)
            end
        end
        if self.damage_type=='物理' then
            def = math.max(target:get('护甲') * (1 - ignore_def*0.01) - source:get('护甲固穿'),0)
        else
            def = math.max(target:get('魔抗') - source:get('魔抗固穿'),0)
        end
        
        if self.hp_remove==nil then
            self:mul(source:get('伤害增幅'))
        end
        self.damage = self.damage / (1 + def*0.01)
    end
    return self.damage
end

function damage:damageCommon()
    local source = self.source
    local target = self.target
    if self.damage_type~='真实' then

        local odd = 0
        local rate = 0
        if self.damage_type=='物理' then
            odd = source:get('暴击几率')
            rate = source:get('暴击伤害')-100
        else
            odd = source:get('法爆几率')
            rate = source:get('法爆伤害')-100
        end
        if self.hp_remove==nil and self['附加']==nil and (self['暴击'] or odd_run(odd)) then
            self['暴击'] = true
            self:mul(rate)
        end
    end
end


-- 扣除生命
function mt:on_cost_life()
    local target = self.target
	local life = target:get '生命'
	if life <= self.current_damage then
		self.real_damage = life
		self:on_kill()
	else
		self.real_damage = self.current_damage
		target:set('生命', life - self.current_damage)
	end
end


-- 吸血
-- ac.game:event '单位-造成伤害'(function(_,source,target,self)
--     local rate = 0
--     if self.damage_type=='物理' then
--         rate = source:get('物理吸血')
--     elseif self.damage_type=='魔法' then
--         rate = source:get('法术吸血')
--     end
--     if rate>0 then
--         local heal = self.current_damage * rate * 0.01
--         source:heal(heal)
--     end
-- end)


-- 伤害显示
local locplayer = ac.player.self
ac.game:event '单位-伤害显示' (function(_, source,damage)
    if damage.current_damage < 1 then
        return
    end
    if damage.dot or damage.aoe then
        return
    end

    local target = damage.target
    local player = source.owner
    -- 被攻击单位对攻击单位不可见
    if not source:is_visible(player) or not player:is_online() then
        return
    end


    local text = format_number1_ex(damage.current_damage)
    if damage['暴击'] then
        text = ('b%s'):format(text)
    end
    if not locplayer:get_data('开关-友伤显示',true) and player~=locplayer then
        text = ''
    end
    if not locplayer:get_data('开关-伤害显示',true) then
        text = ''
    end
    local angle = 0
    --local jump = 0
    -- local key = damage.damage_type .. '次数'
    -- local count = player:get_data(key,0)  + 1
    -- player:set_data(key,count)
    local skill = damage.skill
    ac.ui_tag2{
        string = text,
        target = target:get_point(),
        dmgTy = damage.damage_type,
        speed = 100,
        life = 0.5,
        fade = 0.25,
    }
end)


--击中反馈
ac.game:event '单位-攻击命中'(function(_,source,target)
    if source:is_type('英雄') then
        local point1 = source:get_point()
        local point2 = target:get_point()
        point2:effect{
            model = [[887be44cc027c06fa40bd2116ec9f372.mdx]],
            angle = point2/point1,
            size = 0.5,
            time = 0.1,
        }
        --boom
    end
end)